<template>
  <el-drawer
    @opened="onDrawerOpened"
    v-model="drawerVisible"
    :destroy-on-close="true"
    size="650px"
    :title="`${drawerProps.title}`"
  >
    <div class="drawerVisible-search">
      <el-button @click="addModuler" size="default" type="primary" class="ml10">
        <el-icon style="margin-right: 5px"><CirclePlus /></el-icon>
        新增
      </el-button>
    </div>
    <div class="drawerVisible-form">
      <el-form
        ref="ruleFormRef"
        label-width="150px"
        label-suffix=" :"
        :rules="rules"
        :disabled="drawerProps.isView"
        :model="drawerProps.row"
        :hide-required-asterisk="drawerProps.isView"
      >
        <el-form-item label="疾病名称" prop="strJiBingMingCheng">
          <el-input
            @keydown.enter="strJiBingMingChengLsKeyDown"
            ref="strJiBingMingChengLs"
            v-model="drawerProps.row!.strJiBingMingCheng"
            placeholder="请填写疾病名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="重要异常结果名称" prop="strZhongDaYiChangJieGuo">
          <el-input
            ref="strZhongDaYiChangJieGuoLs"
            @keydown.enter="strZhongDaYiChangJieGuoLsKeyDown"
            v-model="drawerProps.row!.strZhongDaYiChangJieGuo"
            placeholder="请填写重要异常结果名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="疾病类别" prop="strJiBingLeiBie">
          <el-select ref="strJiBingLeiBieLs" v-model="drawerProps.row!.strJiBingLeiBie" placeholder="请选择疾病类别" clearable>
            <el-option v-for="item in jiBingType" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="index" align="center" label="序号" width="60" />
          <el-table-column prop="ZhongYaoYiChangJieGuo" align="center" label="重要异常结果" width="130" />
          <el-table-column prop="JiBingMingCheng" align="center" label="疾病名称" />
          <el-table-column prop="JiBingLeiBie" align="center" label="疾病类别" />
          <el-table-column label="操作">
            <!--     eslint-disable-next-line vue/no-unused-vars-->
            <template #default="scope">
              <el-popconfirm
                confirm-button-text="确定"
                cancel-button-text="取消"
                :icon="InfoFilled"
                icon-color="#626AEF"
                title="确认删除吗?"
                @confirm="confirmEvent(scope.row)"
                @cancel="cancelEvent"
              >
                <template #reference>
                  <el-button size="small" type="danger">删除</el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
    </div>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <!--      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>-->
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="AreaDrawer">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { User } from "@/api/interface";
import { PostKeShiZhongDaJiBingAdd, PostKeShiZhongDaJiBingDel } from "@/api/modules/departmentMaintenance";
import { InfoFilled } from "@element-plus/icons-vue";
import { useFocusInput } from "@/utils";

const rules = reactive({
  strJiBingMingCheng: [{ required: true, message: "请填写疾病名称" }],
  strZhongDaYiChangJieGuo: [{ required: true, message: "请填写重要异常结果名称" }],
  strJiBingLeiBie: [{ required: true, message: "请选择疾病类别" }]
});

const tableData = ref([]);
const jiBingType = ref([
  {
    value: "A类",
    label: "A类"
  },
  {
    value: "B类",
    label: "B类"
  }
]);
interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<User.ResUserList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}
const drawerVisible = ref(false);
const drawerProps = ref<any>({
  isView: false,
  title: "",
  row: {}
});

const addModuler = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      // await drawerProps.value.api!(drawerProps.value.row);
      let obj = {
        strKeShiID: drawerProps.value.row.KeShiID,
        strJiBingMingCheng: drawerProps.value.row.strJiBingMingCheng,
        strZhongDaYiChangJieGuo: drawerProps.value.row.strZhongDaYiChangJieGuo,
        strJiBingLeiBie: drawerProps.value.row.strJiBingLeiBie
      };
      await PostKeShiZhongDaJiBingAdd(obj);
      ElMessage.success({ message: `${drawerProps.value.title}成功！` });
      await requireApi();
      // drawerProps.value.getTableList!();
      // drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

const deleteModuler = async row => {
  let obj = {
    strKeShiID: drawerProps.value.row.KeShiID,
    strJiBingMingCheng: row.JiBingMingCheng.trim(),
    strJiBingLeiBie: row.JiBingLeiBie.trim()
  };
  await PostKeShiZhongDaJiBingDel(obj);
  await requireApi();
};
const confirmEvent = row => {
  deleteModuler(row);
};
const cancelEvent = () => {
  console.log("cancel!");
};
// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  handleClose();
};
const [strJiBingMingChengLs, onDrawerOpened] = useFocusInput();
const [strZhongDaYiChangJieGuoLs, strJiBingMingChengLsKeyDown] = useFocusInput();
const [strJiBingLeiBieLs, strZhongDaYiChangJieGuoLsKeyDown] = useFocusInput();
const requireApi = async () => {
  let obj = {
    strKeShiID: drawerProps.value.row.KeShiID
  };
  const res = await drawerProps.value.api!(obj);
  console.log(res.Data);
  tableData.value = res.Data;
};
const handleClose = async () => {
  await requireApi();
  drawerVisible.value = true;
};
// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();

defineExpose({
  acceptParams
  // queryForm
});
</script>
<style scoped lang="scss">
.drawerVisible-nodules {
  box-sizing: border-box;
  display: inline-flex;
  flex: 0 0 auto;
  align-items: flex-start;
  justify-content: space-between;
  width: 350px;
  height: 32px;
  padding: 0 12px 0 0;
  font-size: var(--el-form-label-font-size);
  line-height: 32px;
  color: var(--el-text-color-regular);
  .drawerVisible-nodules-color {
    color: #009688 !important;
  }
}
.drawerVisible-nodules-color1 {
  margin-left: 10px;
  color: #f78d8d !important;
}
.drawerVisible-search {
  display: flex;
  justify-content: flex-end;
}
.drawerVisible-form {
  margin-top: 10px;
}
</style>
